/*
 * CalendarSeeker.java  1.0 Nov 4, 2013
 *
 * Implement a program that prints out the day of the week for a given
 * day (1..31),
 * month (1..12) and year. For example, for a given input "10 October 2010"
 * it should print to the screen "Sunday"
 */


public class CalendarSeeker {
    public static void main (String[] args) {
        int year = 0;
        int day = 0;
        String month = "";

        String[] inputtedDate = Commons.getStringArrayWithPrompt(Messages.ENTER_DATE);

        day = Integer.parseInt(inputtedDate[0]);
        month = inputtedDate[1];
        year = Integer.parseInt(inputtedDate[2]);

        Month parsedMonth = Month.getValueByName(month);
        int dayCount = parsedMonth.getDayCount(year);
        boolean isValidDay = (dayCount == 0) || (day > dayCount) || (day < 1);

        if (isValidDay) {
            Commons.terminateProgramWithMessage(Messages.INVALID_DATE);
        }

        int dayNumber
            = CalendarSeeker.getDowNumber(year, parsedMonth.getOrder(), day);

        System.out.println(DayOfWeek.getDayNameByNumber(dayNumber));
    }

    /*
     * Devised by Tomohiko Sakamoto in 1993,
     * it is accurate for any Gregorian date
     */
    public static int getDowNumber(int year, int month, int day) {
        final int WEEK_DAY_NUMBER = 7;
        final int VALUE_OF_MONTH[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};

        if (month < 3) {
            year -= 1;
        }

        return (year + (year / 4) - (year / 100) + (year / 400)
                + VALUE_OF_MONTH[month - 1] + day) % WEEK_DAY_NUMBER;
    }
}




